/**
 * 
 */
package com.gopay.common.user.payee.dao.impl;


import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Hibernate;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cipher.utils.SensitiveInfoUtils;
import com.gopay.common.domain.cps.CpsTransReceiver00700;
import com.gopay.common.user.payee.dao.CpsTransReceiver00700QueryDAO;
import com.gopay.common.util.StringTools;

/**
 * @CpsTransReceiver00700QueryDAOImpl.java
 * @author xiefei
 * @2013-3-11 下午2:04:14  www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("cpsTransReceiver00700QueryDAO")
public class CpsTransReceiver00700QueryDAOImpl extends BaseDAOImpl<CpsTransReceiver00700,Long> implements CpsTransReceiver00700QueryDAO {


    /* (non-Javadoc)
     * @see com.gopay.common.cps.dao.tran.CpsTransReceiver00700QueryDAO#findByCustIdPageList(java.lang.String, java.lang.String, int, int)
     */
    @Override
    public List<CpsTransReceiver00700> findByCustIdPageList(String custId,String custType,int pageSize,int pageNo,String rAcctName, String rAcctNum) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CpsTransReceiver00700.class);
        criteria.add(Restrictions.eq("payerCustId",custId));
        criteria.add(Restrictions.eq("payerCustType",Integer.parseInt(custType)));
        /**
         * modified by zyt,对敏感信息的参数用历史密钥加密后，用in查询；
         * 修改后，不支持模糊查询
         * 2017-2-22
         */
        //start+++++++++++
        List<String> hisNameList = null;
        List<String> hisNoList = null;
        
        if(!StringUtils.isBlank(rAcctName)){
        	hisNameList = SensitiveInfoUtils.getAllHisCipherText(rAcctName);
        	criteria.add(Restrictions.in("recvBankAcctName", hisNameList.toArray()));
        }
        if(!StringUtils.isBlank(rAcctNum)){
        	hisNoList = SensitiveInfoUtils.getAllHisCipherText(rAcctNum);
        	criteria.add(Restrictions.in("recvBankAcctNum", hisNoList.toArray()));
        }
       /* if(!StringUtils.isBlank(rAcctName)){
            rAcctName=SensitiveInfoUtils.smartEncrypt(rAcctName);
            if(StringTools.checkSQLLike(rAcctName)){
                criteria.add(Restrictions.sqlRestriction("{alias}.RECV_BANK_ACCT_NAME like ? escape'/'", StringTools.replaceSQLLike(rAcctName), Hibernate.STRING));
            }else{
                criteria.add(Restrictions.like("recvBankAcctName",rAcctName,MatchMode.ANYWHERE));            
                }
            
        }
        if(!StringUtils.isBlank(rAcctNum)){
            rAcctNum=SensitiveInfoUtils.smartEncrypt(rAcctNum);
            if(StringTools.checkSQLLike(rAcctNum)){
                criteria.add(Restrictions.sqlRestriction("{alias}.RECV_BANK_ACCT_NUM like ? escape'/'", StringTools.replaceSQLLike(rAcctNum), Hibernate.STRING));
            }else{
                criteria.add(Restrictions.like("recvBankAcctNum",rAcctNum,MatchMode.ANYWHERE));            
            }
        }*/
        //end+++++++++++++++
        return this.findByCriteria(criteria, pageNo, pageSize);
    }

    /* (non-Javadoc)
     * @see com.gopay.common.cps.dao.tran.CpsTransReceiver00700QueryDAO#findByCustIdCount(java.lang.String, java.lang.String)
     */
    @Override
    public int findByCustIdCount(String custId, String custType,String rAcctName, String rAcctNum) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CpsTransReceiver00700.class);
        criteria.add(Restrictions.eq("payerCustId",custId));
        /**
         * modified by zyt,对敏感信息的参数用历史密钥加密后，用in查询；之后不支持模糊查询
         * 2017-2-24
         */
        //start+++++++++++
        List<String> hisNameList = null;
        List<String> hisNoList = null;
        
        if(!StringUtils.isBlank(rAcctName)){
        	hisNameList = SensitiveInfoUtils.getAllHisCipherText(rAcctName);
        	criteria.add(Restrictions.in("recvBankAcctName", hisNameList.toArray()));
        }
        if(!StringUtils.isBlank(rAcctNum)){
        	hisNoList = SensitiveInfoUtils.getAllHisCipherText(rAcctNum);
        	criteria.add(Restrictions.in("recvBankAcctNum", hisNoList.toArray()));
        }
        /*criteria.add(Restrictions.eq("payerCustType",Integer.parseInt(custType)));
        if(!StringUtils.isBlank(rAcctName)){
            if(StringTools.checkSQLLike(rAcctName)){
                criteria.add(Restrictions.sqlRestriction("{alias}.RECV_BANK_ACCT_NAME like ? escape'/'", StringTools.replaceSQLLike(rAcctName), Hibernate.STRING));
            }else{
                criteria.add(Restrictions.like("recvBankAcctName",rAcctName,MatchMode.ANYWHERE));            
                }
            
        }
        if(!StringUtils.isBlank(rAcctNum)){
            if(StringTools.checkSQLLike(rAcctNum)){
                criteria.add(Restrictions.sqlRestriction("{alias}.RECV_BANK_ACCT_NUM like ? escape'/'", StringTools.replaceSQLLike(rAcctNum), Hibernate.STRING));
            }else{
                criteria.add(Restrictions.like("recvBankAcctNum",rAcctNum,MatchMode.ANYWHERE));            
            }
        }
        return this.findCountByCriteria(criteria);*/
        //end+++++++++++++++
        return this.findCountByCriteria(criteria);
        
    }

}
